<?php

	if (!function_exists('toast')) {
		function toast ($message = '请求成功,暂无预期数据', $code = -1, $data = [], $status = 200) {
			return compact('status', 'code', 'message', 'data');
		}
	}

	if (!function_exists('success')) {
		function success ($data = [], $code = 1, $message = '请求成功,获取预期数据', $status = 200) {
			return compact('status', 'code', 'message', 'data');
		}
	}

	if (!function_exists('addErrorLog')) {
		function addErrorLog ($data, $filename = 'index.log') {
			$folder = Yii::$app->getRuntimePath() . '/record/';
			$dir = $folder . date('Ymd');

			if (!is_dir($dir)) {
				mkdir($dir, 0777, true);
			}

			$path = $dir . '/' . $filename;
			error_log('[ ' . date('Y-m-d H:i:s', time()) . '] : ' . print_r($data, true) . "\r\n", 3, $path);
		}
	}

	if (!function_exists('random')) {
		function random ($length, $numeric = true) {
			$str = null;
			$mixture = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
			$number = '0123456789';
			$strBase = $numeric ? $number : $mixture;
			$max = strlen($strBase) - 1;

			for ($i = 0; $i < $length; $i++) {
				$str .= $strBase[rand(0, $max)];
			}

			return $str;
		}
	}

	if (!function_exists('exists')) {
		function exists ($params) {
			return isset($params) && !empty($params) ? true : false;
		}
	}

